package com.dingwen.treasure.kettle.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * mybatis plus 元对象处理程序
 *
 * @author dingwen
 * @date 2022/04/30
 */
@Configuration(proxyBeanMethods = false)
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    /**
     * 创建时间字段名(实体属性名称)
     */
    private static final String CREATE_TIME_FIELD_NAME = "createTime";
    /**
     * 更新时间字段名(实体属性名称)
     */
    private static final String UPDATE_TIME_FIELD_NAME = "updateTime";

    /**
     * 自定填充新增时间
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        strictInsertFill(metaObject, CREATE_TIME_FIELD_NAME, LocalDateTime.class, LocalDateTime.now());
    }

    /**
     * 自动填充更新时间
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        strictUpdateFill(metaObject, UPDATE_TIME_FIELD_NAME, LocalDateTime.class, LocalDateTime.now());
    }
}
